<?php

class Product_model extends Model {

	function get_products()
	{
		$query = $this->db->get("productos");
		return $query->result();
	}

	function get_orders()
	{
		$query = $this->db->get("order");
		return $query->result();
	}
	
	function get_containers()
	{
		$query = $this->db->get("shipment_info");
		return $query->result();
	}
	
	function get_containersEmpty()
	{
		$query = $this->db->get_where('shipment_info', array('orderID'=> NULL), 20);
		return $query->result();
	}
	

	function getAllPorts()
	{
		$query = $this->db->get("port");
		return $query->result();
	}
	
	
	function getOrderID($name){
	
	$query = $this->db->get_where('order', array('name'=> $name), 1);
	return $query->result();
		
	}
	
	function getOrderInfo($name){
	
	$query = $this->db->get_where('order', array('id'=> $name), 1);
		return $query->result();
		
	}

	function getOrderproduct($name){
	
	$query = $this->db->get_where('order_products', array('orderID'=> $name), 500);
		return $query->result();
		
	}
	
	function getAllConveyance()
	{
		$query = $this->db->get("conveyance");
		return $query->result();
	}
	
	function getPrintInfoID($id)
	{
		$query = $this->db->get_where('shipment_info', array('orderID'=> $id), 1);
		return $query->result();
	}
	
	function getPrintInfoProd($id)
	{
		$query = $this->db->get_where('order_products', array('orderID'=> $id), 1000);
		return $query->result();
	}

	function get_cont_details($id)
	{
		$query = $this->db->get_where('shipment_info', array('id'=> $id), 1);
		return $query->result();
	}

	function get_prod_details($id)
	{
		$query = $this->db->get_where('productos', array('id'=> $id), 1);
		return $query->result();
	}
	
	function get_prod_name($id){
	$query = $this->db->get_where('productos', array('id'=> $id), 1);
		return $query->result();
	}
	
	function get_ports($data, $num = 1)
	{

		$query = $this->db->get_where('port', array('id'=> $data), $num);
		foreach ($query->result() as $row) {
			return $row->name;
		}

	}

	function get_conveyance($data, $num = 1)
	{

		$query = $this->db->get_where('conveyance', array('id'=> $data), $num);
		foreach ($query->result() as $row) {
			return $row->name;
		}

	}
	
	function calculateVolume($id){
	$product = $this->get_prod_details($id);
	foreach($product as $row) {
			$volume = $row->product_H*$row->product_W*$row->product_L;
		}
		return $volume;
	}
	
	function calculateWeight($id){
	$product = $this->get_prod_details($id);
	foreach($product as $row) {
			$weight = $row->product_Weight;
		}
		return $weight;
	}
	
	function finalize_order(){
	$data = array(
               'finalize' => true,
				'volume' => $this->input->post('volume'),
				 'weight' => $this->input->post('weight') 
            );
    
    $this->db->where('id', $this->input->post('orderID'));
	$insert = $this->db->update('order', $data);
	
	return $insert;
	}
	
	function addto_order(){
	
	//variables
	$prod = $this->input->post('productID');
	$order = $this->input->post('orderID');
	$amount = $this->input->post('amount');

	
	//checking if the product already exists in that order
	$query = $this->db->query("SELECT * FROM order_products WHERE productID = '$prod' && orderID = '$order'");
	$number = $query->num_rows();
	
	if($number < 1){
	
	$data = array(
			'productID' => $prod,
			'orderID' => $order,
			'Amount' => $amount
		);
		$insert = $this->db->insert('order_products', $data);
		return $insert;
		}else{
		
		$query = $this->db->get_where('order_products', array('orderID'=> $order, 'productID' => $prod), 20);
		foreach ($query->result() as $row) {
			$dbAmount = $row->Amount;
		}
		
		$newAmount = $amount+$dbAmount;
		$data = array(
               'amount' => $newAmount
            );
		$this->db->where('productID', $prod);
		$this->db->where('orderID', $order);
		$insert = $this->db->update('order_products', $data);
		return $insert;
		
		}
	}
	
	function addto_cont($cont, $order){
		
	//update shipment info table	
	$data = array(
               'orderID' => $order
            );
		$this->db->where('id', $cont);
		$insert = $this->db->update('shipment_info', $data);
		
	//add container ID to products
	$data = array(
               'shipment_info_ref' => $cont
            );
	$this->db->where('orderID', $order);
	$insert = $this->db->update('order_products', $data);
	
	return $insert;
	}
	
	function add_volume($id, $volume, $order){
	$data = array(
				'Volume' => $volume
            );
    
    $this->db->where('productID', $id);
    $this->db->where('orderID', $order);
	$insert = $this->db->update('order_products', $data);
	
	return $insert;
	}

	function edit_product()
	{
		$data = array(
			'upc' => $this->input->post('upc'),
			'desc' => $this->input->post('desc'),
			'model' => $this->input->post('model'),
			'product_MC' => $this->input->post('product_MC'),
			'product_H' => $this->input->post('product_H'),
			'product_W' => $this->input->post('product_W'),
			'product_L' => $this->input->post('product_L'),
			'product_Weight' => $this->input->post('product_Weight')
		);

		$this->db->where('id', $this->input->post('prodID'));
		$insert = $this->db->update('productos', $data);
		return $insert;
	}

	function edit_container()
	{
		$data = array(
			'port' => $this->input->post('port'),
			'date' => $this->input->post('date'),
			'conveyance' => $this->input->post('conveyance'),
			'container_serial' => $this->input->post('container_serial'),
			'bar1' => $this->input->post('bar1'),
			'seal1' => $this->input->post('seal1'),
			'seal2' => $this->input->post('seal2'),
			'vessel_name' => $this->input->post('vessel_name'),
			'ref' => $this->input->post('ref'),
			'booking' => $this->input->post('booking'),
			'port_unload' => $this->input->post('port_unload'),
			'sum_insured' => $this->input->post('sum_insured')
		);

		$this->db->where('id', $this->input->post('containerID'));
		$insert = $this->db->update('shipment_info', $data);
		return $insert;
	}

	function edit_order()
	{
		$data = array(
			'port' => $this->input->post('port'),
			'date' => $this->input->post('date'),
			'conveyance' => $this->input->post('conveyance'),
			'container_serial' => $this->input->post('container_serial'),
			'bar1' => $this->input->post('bar1'),
			'seal1' => $this->input->post('seal1'),
			'seal2' => $this->input->post('seal2'),
			'vessel_name' => $this->input->post('vessel_name'),
			'ref' => $this->input->post('ref'),
			'booking' => $this->input->post('booking'),
			'port_unload' => $this->input->post('port_unload'),
			'sum_insured' => $this->input->post('sum_insured')
		);

		$this->db->where('id', $this->input->post('containerID'));
		$insert = $this->db->update('shipment_info', $data);
		return $insert;
	}

	function create_product()
	{
		$new_product_data = array(
			'upc' => $this->input->post('upc'),
			'desc' => $this->input->post('desc'),
			'model' => $this->input->post('model'),
			'product_MC' => $this->input->post('product_MC'),
			'product_H' => $this->input->post('product_H'),
			'product_W' => $this->input->post('product_W'),
			'product_L' => $this->input->post('product_L'),
			'product_Weight' => $this->input->post('product_Weight')
		);

		$insert = $this->db->insert('productos', $new_product_data);
		return $insert;
	}
	
	function create_order()
	{
	$date = $this->input->post('month').$this->input->post('day').$this->input->post('year');
		$new_product_data = array(
			'name' => $this->input->post('order'),
			'date'=> $date
		);

		$query = $this->db->insert('order', $new_product_data);
		

		return $query;
	}

	function create_container()
	{
		$date = $this->input->post('month').$this->input->post('day').$this->input->post('year');
		$new_product_data = array(
			'port' => $this->input->post('port'),
			'date' => $date,
			'conveyance' => $this->input->post('conveyance'),
			'container_serial' => $this->input->post('container_serial'),
			'bar1' => $this->input->post('bar1'),
			'seal1' => $this->input->post('seal1'),
			'seal2' => $this->input->post('seal2'),
			'vessel_name' => $this->input->post('vessel_name'),
			'ref' => $this->input->post('ref'),
			'booking' => $this->input->post('booking'),
			'port_unload' => $this->input->post('port_unload'),
			'sum_insured' => $this->input->post('sum_insured')
		);

		$insert = $this->db->insert('shipment_info', $new_product_data);
		return $insert;
	}

	function remove_fromOrder($id){
		
		$this->db->where('productID', $id);
		$this->db->where('orderID', $this->uri->segment(3));
		$this->db->delete('order_products');
	
	
	}

	function delete_product()
	{
		$this->db->where('id', $this->uri->segment(3));
		$this->db->delete('productos');

	}

	function delete_container()
	{
		$this->db->where('id', $this->uri->segment(3));
		$this->db->delete('shipment_info');

	}
	function delete_order()
	{
		$id = $this->uri->segment(3);
		
		//delete from order_products
		$this->db->where('orderID', $id);
		$this->db->delete('order_products');
		
		//delete from order
		$this->db->where('id', $id);
		$this->db->delete('order');
		
		//take off value from any shipment_info
		$data = array(
				'orderID' => NULL
            );
    
    $this->db->where('orderID', $id);
	$insert = $this->db->update('shipment_info', $data);
	
	return $insert;

	}
}